# -*- coding: utf-8 -*-
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.Qt import QMainWindow
from PyQt5.QtWidgets import QMessageBox

import connector.driver
import dao.UserDao
from pages.RegisterPage import RegisterPage
import tools
from tools import MessageBoxTools
from pages import ManagementMainPage, UserMainPage


class LoginPage(QMainWindow):

    def __init__(self):
        super().__init__()
        self.register_page = None
        self.setupUi()
        self.show()
        self.driver = connector.driver.DatabaseDriver()

    def setupUi(self):
        self.setObjectName("MainWindow")
        self.resize(945, 705)
        self.centralwidget = QtWidgets.QWidget(self)
        self.centralwidget.setObjectName("centralwidget")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(140, 70, 681, 131))
        font = QtGui.QFont()
        font.setPointSize(65)
        font.setBold(True)
        font.setWeight(75)
        self.label.setFont(font)
        self.label.setStyleSheet("color: rgb(255, 255, 255);")
        self.label.setTextFormat(QtCore.Qt.AutoText)
        self.label.setObjectName("label")
        self.listWidget = QtWidgets.QListWidget(self.centralwidget)
        self.listWidget.setGeometry(QtCore.QRect(-10, -40, 971, 721))
        self.listWidget.setMinimumSize(QtCore.QSize(941, 711))
        self.listWidget.setFocusPolicy(QtCore.Qt.ClickFocus)
        self.listWidget.setStyleSheet("background-image: url(views/imgs/LoginPageBG.png);")
        self.listWidget.setObjectName("listWidget")
        self.username_edittext = QtWidgets.QLineEdit(self.centralwidget)
        self.username_edittext.setGeometry(QtCore.QRect(220, 250, 461, 61))
        font = QtGui.QFont()
        font.setPointSize(18)
        self.username_edittext.setFont(font)
        self.username_edittext.setObjectName("username_edittext")
        self.password_edittext = QtWidgets.QLineEdit(self.centralwidget)
        self.password_edittext.setGeometry(QtCore.QRect(220, 360, 461, 61))
        font = QtGui.QFont()
        font.setPointSize(18)
        self.password_edittext.setFont(font)
        self.password_edittext.setObjectName("password_edittext")
        self.label_2 = QtWidgets.QLabel(self.centralwidget)
        self.label_2.setGeometry(QtCore.QRect(100, 260, 81, 41))
        font = QtGui.QFont()
        font.setPointSize(21)
        font.setBold(True)
        font.setWeight(75)
        self.label_2.setFont(font)
        self.label_2.setStyleSheet("color: rgb(255, 255, 255);")
        self.label_2.setObjectName("label_2")
        self.label_3 = QtWidgets.QLabel(self.centralwidget)
        self.label_3.setGeometry(QtCore.QRect(100, 370, 81, 41))
        font = QtGui.QFont()
        font.setPointSize(21)
        font.setBold(True)
        font.setWeight(75)
        self.label_3.setFont(font)
        self.label_3.setStyleSheet("color: rgb(255, 255, 255);")
        self.label_3.setObjectName("label_3")
        self.login_btn = QtWidgets.QPushButton(self.centralwidget)
        self.login_btn.setGeometry(QtCore.QRect(710, 250, 181, 171))
        font = QtGui.QFont()
        font.setPointSize(30)
        font.setBold(True)
        font.setWeight(75)
        self.login_btn.setFont(font)
        self.login_btn.setStyleSheet("color: rgb(200, 200, 200);")
        self.login_btn.setObjectName("login_btn")
        self.register_button = QtWidgets.QPushButton(self.centralwidget)
        self.register_button.setGeometry(QtCore.QRect(280, 560, 351, 71))
        font = QtGui.QFont()
        font.setPointSize(16)
        self.register_button.setFont(font)
        self.register_button.setObjectName("register_button")
        self.listWidget.raise_()
        self.label.raise_()
        self.username_edittext.raise_()
        self.password_edittext.raise_()
        self.label_2.raise_()
        self.label_3.raise_()
        self.login_btn.raise_()
        self.register_button.raise_()
        self.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(self)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 945, 26))
        self.menubar.setObjectName("menubar")
        self.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(self)
        self.statusbar.setObjectName("statusbar")
        self.setStatusBar(self.statusbar)

        self.retranslateUi(self)
        QtCore.QMetaObject.connectSlotsByName(self)
        self.connect_bind()

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "图书管理系统"))
        self.label.setText(_translate("MainWindow", "图书管理系统"))
        self.username_edittext.setPlaceholderText(_translate("MainWindow", "请输入账户"))
        self.password_edittext.setPlaceholderText(_translate("MainWindow", "请输入密码"))
        self.label_2.setText(_translate("MainWindow", "账号"))
        self.label_3.setText(_translate("MainWindow", "密码"))
        self.login_btn.setText(_translate("MainWindow", "登录"))
        self.register_button.setText(_translate("MainWindow", "没有账户？请注册"))

    # 事件绑定关联
    def connect_bind(self):
        self.login_btn.clicked.connect(self.login_btn_clicked)
        self.register_button.clicked.connect(self.register_btn_clicked)

    # 登录按钮点击事件
    def login_btn_clicked(self):
        if self.username_edittext.text() == "" or self.password_edittext.text() == "":
            MessageBoxTools.warning(title="输入错误", text="请输入用户名或密码！", buttons=QMessageBox.Ok)
            return
        else:
            # 说明用户输入了账户和密码
            result: tuple = dao.UserDao.login(self.driver, self.username_edittext.text(), self.password_edittext.text())
            # result[0] 用户id result[1] 用户角色
            if result is not None and len(result) == 2:
                user_id: int = result[0]
                if result[1] == 0:
                    # 表明是管理员
                    MessageBoxTools.information("登录成功", "尊敬的管理员，欢迎你使用图书管理系统",
                                                buttons=QMessageBox.Ok)
                    self.management_main_page = ManagementMainPage.ManagementMainPage(user_id)  # 启动管理员界面
                else:
                    # 表明是普通用户
                    MessageBoxTools.information("登录成功", "尊敬的读者，欢迎你使用图书系统", buttons=QMessageBox.Ok)
                    self.user_main_page = UserMainPage.UserMainPage(user_id)  # 启动用户借阅界面
                self.close()  # 关闭当前窗口
            else:
                MessageBoxTools.warning(title="警告", text="用户名或密码错误！", buttons=QMessageBox.Ok)
                return
    # 注册按钮触发事件
    def register_btn_clicked(self):
        self.register_page = RegisterPage()
        self.close()

    def closeEvent(self, a0: QtGui.QCloseEvent) -> None:
        self.driver.close()

    def keyPressEvent(self, key: QtGui.QKeyEvent) -> None:
        if key.key() == 16777220:
            # 判断按下的是否是回车键
            self.login_btn_clicked()